
package com.lzx.iteam.util;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Vibrator;

public class SendCardShakeListener implements SensorEventListener {


    private SensorManager mSensorMgr;

    private OnShakeListener mShakeListener;

    // 速度阈值，当摇晃速度达到这值后产生作用
    private static int SPEED_SHRESHOLD = 2000;

    // 两次检测的时间间隔
    private static final int UPTATE_INTERVAL_TIME = 70;
    
    private static final int LAST_ONSHAKE_TIME = 1000;

    // 手机上一个位置时重力感应坐标
    private float lastX;

    private float lastY;

    private float lastZ;

    // 上次检测时间
    private long lastUpdateTime;
    private long lastOnShake;

    private Context mContext;

    float mAxisX;

    float mAxisY;

    float mAxisZ;

    private double mTotalForcePrev;
    
    private Vibrator vibrator;

    public float getAxisX() {
        return mAxisX;
    }

    public interface OnShakeListener {
        // public void onShake(float axisX, float axisY, float axisZ);
        public void onShake();
        // public void onShakeHorizontal();
        // public void onShakeVertical();
    }

    public SendCardShakeListener(Context context) {
        mContext = context;
        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        if (Build.MANUFACTURER.trim().equalsIgnoreCase("samsung")) {
            SPEED_SHRESHOLD = 1500;
        } else if (Build.BRAND.trim().equalsIgnoreCase("Coolpad")
                || Build.MANUFACTURER.trim().equalsIgnoreCase("htc")) {
            SPEED_SHRESHOLD = 3500;
        } else if (Build.BRAND.trim().equalsIgnoreCase("Xiaomi")){
            SPEED_SHRESHOLD = 850;
        } else if (Build.BRAND.trim().equalsIgnoreCase(("google"))){
            SPEED_SHRESHOLD = 1550;
        } else if (Build.BRAND.trim().equalsIgnoreCase("HUAWEI")){
            SPEED_SHRESHOLD = 850;
        }
        resume();
    }

    public void setOnShakeListener(OnShakeListener listener) {
        mShakeListener = listener;
    }

    public void resume() {
        mSensorMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        if (mSensorMgr == null) {
            throw new UnsupportedOperationException("Sensors not supported");
        }

        boolean supported = mSensorMgr.registerListener(this,
                mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_UI);
        if (!supported) {
//            mSensorMgr.unregisterListener(this);
//            Toast.makeText(mContext, "Accelerometer not supported", Toast.LENGTH_LONG).show();
        }
    }

    public void pause() {
        if (mSensorMgr != null) {
            mSensorMgr.unregisterListener(this);
            mSensorMgr = null;
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
            return;
        }

        // 现在检测时间
        long currentUpdateTime = System.currentTimeMillis();
        // 两次检测的时间间隔
        long timeInterval = currentUpdateTime - lastUpdateTime;
        // 判断是否达到了检测时间间隔
        if (timeInterval < UPTATE_INTERVAL_TIME)
            return;
        // 现在的时间变成last时间
        lastUpdateTime = currentUpdateTime;
        // 获得x,y,z坐标
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        // 获得x,y,z的变化值
        float deltaX = x - lastX;
        float deltaY = y - lastY;
        float deltaZ = z - lastZ;
        // 将现在的坐标变成last坐标
        lastX = x;
        lastY = y;
        lastZ = z;
        double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + 0.2 * deltaZ * deltaZ)
                / timeInterval * 10000;
        // 达到速度阀值，发出提示
        if (speed >= SPEED_SHRESHOLD) {
            if(currentUpdateTime-lastOnShake>LAST_ONSHAKE_TIME){
                vibrator.vibrate(500);
                mShakeListener.onShake();
                lastOnShake = currentUpdateTime;
            }
            
        }

        /*
         * double forceThreshHold = 2.5f; double totalForce = 0.0f; totalForce
         * += Math.pow(event.values[SensorManager.DATA_X] /
         * SensorManager.GRAVITY_EARTH, 2.0); totalForce +=
         * Math.pow(event.values[SensorManager.DATA_Y] /
         * SensorManager.GRAVITY_EARTH, 2.0); totalForce +=
         * Math.pow(event.values[SensorManager.DATA_Z] /
         * SensorManager.GRAVITY_EARTH, 2.0); totalForce =
         * Math.sqrt(totalForce); if ((totalForce < forceThreshHold) &&
         * (mTotalForcePrev > forceThreshHold)) { mShakeListener.onShake(); }
         * mTotalForcePrev = totalForce;
         */
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
